﻿// -----------------------------------------------------------------------
// <copyright file="FactStudentCertificateRepository.cs" company="AGR">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AGR.Data.Repository.Contract
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using AGR.Model;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class FactStudentCertificateRepository : AbstractRepository<FactStudentCertificate>
    {
        public FactStudentCertificateRepository(ContextHelper contextHelper)
        {
            base._contextHelper = contextHelper;
            base._context = _contextHelper.GetContext();
        }

        public override IQueryable<FactStudentCertificate> GetAll()
        {
            var result = (from x in base._context.FactStudentCertificates
                          select new FactStudentCertificate
                          {
                              Id = x.Id,
                              StudentCode = x.StudentCode,
                              CertificateCode = x.CertificateCode,
                              DateCreated = x.DateCreated,
                              UserCreated = x.UserCreated,
                              DateModified = x.DateModified,
                              UserModified = x.UserModified
                          });

            return result;
        }

        public override void Add(FactStudentCertificate entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.FactStudentCertificate entityToAdd = new Context.FactStudentCertificate
            {
                StudentCode = entity.StudentCode,
                CertificateCode = entity.CertificateCode,
                DateCreated = DateTime.Now,
                UserCreated = entity.UserCreated,
                DateModified = null,
                UserModified = null
            };

            base._context.FactStudentCertificates.InsertOnSubmit(entityToAdd);
        }

        public override void Update(FactStudentCertificate entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.FactStudentCertificate entityToUpdate = base._context.FactStudentCertificates.Single(x => x.CertificateCode.Equals(entity.CertificateCode));

            entityToUpdate.StudentCode = entity.StudentCode;
            entityToUpdate.CertificateCode = entity.CertificateCode;
            entityToUpdate.DateModified = DateTime.Now;
            entityToUpdate.UserModified = entity.UserModified;
        }

        public override void Delete(FactStudentCertificate entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.FactStudentCertificate entityToDelete = base._context.FactStudentCertificates.Single(x => x.CertificateCode.Equals(entity.CertificateCode));

            base._context.FactStudentCertificates.DeleteOnSubmit(entityToDelete);
        }
    }
}
